package com.telenav.app;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;

public class WelcomeActivity extends Activity {

	private ViewPager viewPager;

	private ArrayList<View> pageViews;

	private ViewGroup viewPictures;

	private ImageView imageView;
	
	private int selectedIndex;

	private int[] welPictures = { R.drawable.welcome1, R.drawable.welcome2 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		LayoutInflater inflater = getLayoutInflater();
		pageViews = new ArrayList<View>();

		for (int i = 0; i < welPictures.length; i++) {
			imageView = new ImageView(WelcomeActivity.this);
			imageView.setLayoutParams(new LayoutParams(10, 10));
			imageView.setPadding(10, 0, 10, 0);
			imageView.setBackgroundResource(welPictures[i]);
			imageView.setOnClickListener(new ItemOnClickListener());
			pageViews.add(imageView);
		}

		viewPictures = (ViewGroup) inflater.inflate(R.layout.activitywelcome,
				null);

		viewPager = (ViewPager) viewPictures.findViewById(R.id.viewPager);

		setContentView(viewPictures);

		viewPager.setAdapter(new NavigationPageAdapter());
	}

	class NavigationPageAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return pageViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(View container, int position) {
			selectedIndex = position;
			((ViewPager) container).addView(pageViews.get(position));
			return pageViews.get(position);
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			selectedIndex = 0;
			((ViewPager) container).removeView(pageViews.get(position));
		}

	}

	class ItemOnClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {

			if(selectedIndex == welPictures.length -1)
			{
				startbutton(v);
			}
			return;
		}

	}

	public void startbutton(View v) {
		Intent intent = new Intent(WelcomeActivity.this, HomeActivity.class);
		startActivity(intent);
		WelcomeActivity.this.finish();
	}

}
